package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements IOnDelListener{
    private ListView Stulistview;
    private ArrayList<Student> studentArrayList=new ArrayList<>();
    private StuAdapter adapter;
    private Button btnAddStu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Stulistview=findViewById(R.id.listView_stu);
        initStuArrayList();

        adapter=new StuAdapter(MainActivity.this,studentArrayList,MainActivity.this);
        Stulistview.setAdapter(adapter);

        Stulistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this,
                        "姓名"+studentArrayList.get(i).getName()
                                + "年龄"+studentArrayList.get(i).getAge(),
                        Toast.LENGTH_SHORT).show();

                Intent intent=new Intent();
                intent.setClass(MainActivity.this,EditActivity.class);
            }
        });
        
        btnAddStu=findViewById(R.id.btn_AddStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivityForResult(intent,1001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("MainActivity","requestCode="+requestCode+"resultCode="+requestCode);

        if(data==null){
            return;
        }

        String name=data.getStringExtra("Name");
        int age=data.getIntExtra("Age",0);
        int imgId=data.getIntExtra("ImgId",R.drawable.dog1);

        Student student=new Student(name,age,imgId);
        studentArrayList.add(student);
        adapter.notifyDataSetChanged();;
    }

    private void initStuArrayList() {
        Student s1=new Student("甲",1,R.drawable.dog1);
        Student s2=new Student("乙",2,R.drawable.dog2);
        Student s3=new Student("秉",3,R.drawable.dog3);
        Student s4=new Student("丁",4,R.drawable.dog4);
        Student s5=new Student("他",5,R.drawable.dog5);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);

        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    public void del(int i){
        Log.i("MainAtcity","del"+i);
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }
}
